package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonObject;
import com.wtwd.campus.entity.SysCustomer;
import com.wtwd.campus.model.vo.PageDataVO;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.description.NamedElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ldaoliang
 * @Date create in 9:22 2021/7/9
 * @Description
 */
@Api(tags = "第三方客户")
@RestController
@RequestMapping(value = "/sys-customer")
public class SysCustomerController {

    private static final Logger logger = LoggerFactory.getLogger(SysCustomerController.class);

    @Autowired
    private CustomerService customerService;

    /**
     * 客户列表
     *
     * @param currPage
     * @param pageSize
     * @param customerName
     * @return
     */
    @ApiOperation(value = "查询客户分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currPage", value = "当前页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页面容量", required = true),
            @ApiImplicitParam(name = "customerName", value = "客户名称", required = true)
    })
    @GetMapping("/customerList")
    public ResultVO<PageDataVO<SysCustomer>> customerList(@RequestParam(defaultValue = "1") Integer currPage,
                                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                                         @RequestParam(defaultValue = "") String customerName) {
        logger.info("customerList : currPage = {}, pageSize = {}, customerName = {}", currPage, pageSize, customerName);
        Page<SysCustomer> page = new Page<>(currPage, pageSize);
        IPage<SysCustomer> sysCustomerList = customerService.getSysCustomerList(page, customerName);
        return ResultVO.ok(sysCustomerList);
    }

    /**
     * 添加客户
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "添加客户")
    @ApiImplicitParam(name = "customerName", value = "客户名", required = true)
    @PostMapping("/addCustomer")
    public ResultVO addCustomer(@RequestBody JSONObject jsonObject) {
        logger.info("addCustomer : param = {}", jsonObject);
        return customerService.addCustomer(jsonObject.getString("customerName"));
    }

    /**
     * 修改客户名称
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "更新客户名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户Id", required = true),
            @ApiImplicitParam(name = "customerName", value = "客户名", required = true)
    })
    @PutMapping("/updateCustomer")
    public ResultVO updateCustomer(@RequestBody JSONObject jsonObject) {
        logger.info("updateCustomer : param = {}",jsonObject);
        Integer customerId = jsonObject.getInteger("customerId");
        String customerName = jsonObject.getString("customerName");
        return customerService.updateCustomer(customerId,customerName);
    }

    /**
     * 开启关闭客户
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "开启禁用客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "客户Id", required = true),
            @ApiImplicitParam(name = "onOff", value = "启用禁用标识", required = true)
    })
    @PutMapping("/startDisableCustomer")
    public ResultVO startDisableCustomer(@RequestBody JSONObject jsonObject){
        logger.info("startDisableCustomer : param = {}",jsonObject);
        Integer customerId = jsonObject.getInteger("customerId");
        Integer onOff = jsonObject.getInteger("onOff");
        return customerService.startDisableCustomer(customerId,onOff);
    }

}
